package com.jyh.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.jyh.common.PublicField;
import com.jyh.common.R;
import com.jyh.pojo.AddressBook;
import com.jyh.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查看所有地址
     * @return
     */
    @GetMapping("/list")
    public R<List<AddressBook>> list(){
        Long currentId = PublicField.getCurrentId();
        LambdaQueryWrapper<AddressBook> lqw=new LambdaQueryWrapper<>();
        lqw.eq(currentId!=null,AddressBook::getUserId,currentId);
        List<AddressBook> list = addressBookService.list(lqw);
        return R.success(list);
    }

    /**
     * 添加地址
     * @param addressBook
     * @return
     */
    @PostMapping
    public R<String> add(@RequestBody AddressBook addressBook){
        //log.info("address:{}",addressBook);
        addressBook.setUserId(PublicField.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("success");
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    public R<AddressBook> addressDefault(@RequestBody AddressBook addressBook){
        //log.info("id:{}",addressBook.getId());

        //根据当前用户id将其所有地址的默认地址=0
        LambdaUpdateWrapper<AddressBook> lqw=new LambdaUpdateWrapper<>();
        lqw.eq(AddressBook::getUserId,PublicField.getCurrentId());
        lqw.set(AddressBook::getIsDefault,0);
        addressBookService.update(lqw);

        addressBook.setIsDefault(1);
        //当前地址有id
        addressBookService.updateById(addressBook);

        return R.success(addressBook);
    }

    /**
     * 查询默认地址
     * @return
     */
    @GetMapping("/default")
    public R<AddressBook> getDefaultAddress(){
        LambdaQueryWrapper<AddressBook> lqw=new LambdaQueryWrapper<>();

        lqw.eq(AddressBook::getUserId,PublicField.getCurrentId());
        lqw.eq(AddressBook::getIsDefault,1);

        AddressBook addressBook = addressBookService.getOne(lqw);
        if(addressBook!=null){
            return R.success(addressBook);
        }

        return R.error("暂未设置默认地址");
    }

    /**
     * 编辑查找地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<AddressBook> editAddress(@PathVariable Long id){
        //log.info("id:{}",id);
        AddressBook addressBook = addressBookService.getById(id);
        return R.success(addressBook);
    }

    /**
     * 更新地址
     * @param addressBook
     * @return
     */
    @PutMapping
    public R<String> updateAddress(@RequestBody AddressBook addressBook){
        //log.info(addressBook.toString());
        addressBookService.updateById(addressBook);
        return R.success("success");
    }

    /**
     * 删除地址
     * @param ids
     * @return
     */
    @DeleteMapping
    public R<String> deleteAddress(@RequestParam  Long ids){
        log.info("id:{}",ids);
        addressBookService.removeById(ids);
        return R.success("success");
    }


}
